/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFrameMain.java
 *
 * Created on 31/10/2011, 15:50:22
 */
package br.com.mocamorena.view;

import br.com.mocamorena.model.Usuario;
import br.com.mocamorena.view.dialog.CategoriaDialog;
import br.com.mocamorena.view.dialog.ChangePasswordDialog;
import br.com.mocamorena.view.dialog.ClienteDialog;
import br.com.mocamorena.view.dialog.ConsultaCategoriaDialog;
import br.com.mocamorena.view.dialog.ConsultaClienteDialog;
import br.com.mocamorena.view.dialog.ConsultaEnderecoDialog;
import br.com.mocamorena.view.dialog.ConsultaUsuarioDialog;
import br.com.mocamorena.view.dialog.CreateUsuarioDialog;
import br.com.mocamorena.view.dialog.EnderecoDialog;
import br.com.mocamorena.view.dialog.SobreDialog;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Diego
 */
public class MenuPrincipalJFrame extends javax.swing.JFrame {

    private Usuario user;

    public MenuPrincipalJFrame(Usuario user) {
        createActions();
        initComponents();
        setUser(user);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setLocationRelativeTo(null);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        clienteButton = new javax.swing.JButton(clienteConsultaAction);
        jSeparator9 = new javax.swing.JToolBar.Separator();
        produtoButton = new javax.swing.JButton(produtoConsultaAction);
        categoriaButton = new javax.swing.JButton(categoriaConsultaAction);
        jPanel1 = new javax.swing.JPanel();
        contaUsuarioButton = new javax.swing.JButton(contaUsuarioAction);
        ajudaButton = new javax.swing.JButton(sobreAction);
        jPanel2 = new javax.swing.JPanel();
        usuarioLabel = new javax.swing.JLabel();
        jSeparator6 = new javax.swing.JSeparator();
        jMenuBar1 = new javax.swing.JMenuBar();
        arquivoMainMenu = new javax.swing.JMenu();
        logoutMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        sairMenuItem = new javax.swing.JMenuItem(finallyApplicationAction);
        controleMainMenu = new javax.swing.JMenu();
        clienteMenuItem = new javax.swing.JMenuItem(clienteConsultaAction);
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        produtoMenuItem = new javax.swing.JMenuItem(produtoConsultaAction);
        categoriaMenuItem = new javax.swing.JMenuItem(categoriaConsultaAction);
        jSeparator8 = new javax.swing.JPopupMenu.Separator();
        enderecoMenuItem = new javax.swing.JMenuItem(enderecoConsultaAction);
        configuracoesMainMenu = new javax.swing.JMenu();
        contaUsuarioMenuItem = new javax.swing.JMenuItem(contaUsuarioAction);
        jSeparator7 = new javax.swing.JPopupMenu.Separator();
        usuariosMenuItem = new javax.swing.JMenuItem(usuarioConsultaAction);
        ajudaMainMenu = new javax.swing.JMenu();
        sobreMenuItem = new javax.swing.JMenuItem(sobreAction);

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Moça Morena 1.0 - Menu Principal");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jToolBar1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setBorderPainted(false);

        clienteButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/clients64x64.png"))); // NOI18N
        clienteButton.setToolTipText("Controle de Clientes");
        clienteButton.setFocusable(false);
        clienteButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        clienteButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(clienteButton);
        jToolBar1.add(jSeparator9);

        produtoButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/clients64x64.png"))); // NOI18N
        produtoButton.setToolTipText("Controle de Produtos");
        produtoButton.setFocusable(false);
        produtoButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        produtoButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(produtoButton);

        categoriaButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/categoria48x48.png"))); // NOI18N
        categoriaButton.setToolTipText("Controle de Categorias");
        categoriaButton.setFocusable(false);
        categoriaButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        categoriaButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(categoriaButton);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 440, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 64, Short.MAX_VALUE)
        );

        jToolBar1.add(jPanel1);

        contaUsuarioButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/useraccount48x48.png"))); // NOI18N
        contaUsuarioButton.setToolTipText("Alterar senha da conta de usuário");
        contaUsuarioButton.setFocusable(false);
        contaUsuarioButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        contaUsuarioButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(contaUsuarioButton);

        ajudaButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/help48x48.png"))); // NOI18N
        ajudaButton.setToolTipText("Ajuda");
        ajudaButton.setFocusable(false);
        ajudaButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        ajudaButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(ajudaButton);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        usuarioLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        usuarioLabel.setText("Usuário logado no sistema:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(408, Short.MAX_VALUE)
                .addComponent(usuarioLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 339, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jSeparator6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 757, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jSeparator6, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(usuarioLabel)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        arquivoMainMenu.setMnemonic('a');
        arquivoMainMenu.setLabel("Arquivo");

        logoutMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
        logoutMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/logout.png"))); // NOI18N
        logoutMenuItem.setText("Fechar sessão");
        logoutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutMenuItemActionPerformed(evt);
            }
        });
        arquivoMainMenu.add(logoutMenuItem);
        arquivoMainMenu.add(jSeparator1);

        sairMenuItem.setMnemonic('s');
        sairMenuItem.setText("Sair");
        arquivoMainMenu.add(sairMenuItem);

        jMenuBar1.add(arquivoMainMenu);

        controleMainMenu.setMnemonic('c');
        controleMainMenu.setText("Controle");

        clienteMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
        clienteMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/clients32x32.png"))); // NOI18N
        clienteMenuItem.setText("Cliente...");
        controleMainMenu.add(clienteMenuItem);
        controleMainMenu.add(jSeparator2);

        produtoMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3, 0));
        produtoMenuItem.setText("Produto...");
        controleMainMenu.add(produtoMenuItem);

        categoriaMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, 0));
        categoriaMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/categoria32x32.png"))); // NOI18N
        categoriaMenuItem.setText("Categoria...");
        controleMainMenu.add(categoriaMenuItem);
        controleMainMenu.add(jSeparator8);

        enderecoMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        enderecoMenuItem.setText("Endereço...");
        controleMainMenu.add(enderecoMenuItem);

        jMenuBar1.add(controleMainMenu);

        configuracoesMainMenu.setMnemonic('o');
        configuracoesMainMenu.setText("Configurações");

        contaUsuarioMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
        contaUsuarioMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/useraccount32x32.png"))); // NOI18N
        contaUsuarioMenuItem.setText("Conta de usuário...");
        configuracoesMainMenu.add(contaUsuarioMenuItem);
        configuracoesMainMenu.add(jSeparator7);

        usuariosMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_U, java.awt.event.InputEvent.CTRL_MASK));
        usuariosMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/usuarios32x32.png"))); // NOI18N
        usuariosMenuItem.setText("Usuários...");
        configuracoesMainMenu.add(usuariosMenuItem);

        jMenuBar1.add(configuracoesMainMenu);

        ajudaMainMenu.setMnemonic('u');
        ajudaMainMenu.setText("Ajuda");

        sobreMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/help32x32.png"))); // NOI18N
        sobreMenuItem.setMnemonic('o');
        sobreMenuItem.setText("Sobre o Sistema");
        ajudaMainMenu.add(sobreMenuItem);

        jMenuBar1.add(ajudaMainMenu);

        setJMenuBar(jMenuBar1);
        setExtendedState(JFrame.MAXIMIZED_BOTH);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 757, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 312, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void logoutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutMenuItemActionPerformed
        dispose();
        LoginJFrame login = new LoginJFrame();
        login.setVisible(true);
    }//GEN-LAST:event_logoutMenuItemActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        finallyApplicationAction.actionPerformed(null);
    }//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            System.out.println(ex);
        }
        //</editor-fold>
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MenuPrincipalJFrame(null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ajudaButton;
    private javax.swing.JMenu ajudaMainMenu;
    private javax.swing.JMenu arquivoMainMenu;
    private javax.swing.JButton categoriaButton;
    private javax.swing.JMenuItem categoriaMenuItem;
    private javax.swing.JButton clienteButton;
    private javax.swing.JMenuItem clienteMenuItem;
    private javax.swing.JMenu configuracoesMainMenu;
    private javax.swing.JButton contaUsuarioButton;
    private javax.swing.JMenuItem contaUsuarioMenuItem;
    private javax.swing.JMenu controleMainMenu;
    private javax.swing.JMenuItem enderecoMenuItem;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JPopupMenu.Separator jSeparator7;
    private javax.swing.JPopupMenu.Separator jSeparator8;
    private javax.swing.JToolBar.Separator jSeparator9;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JMenuItem logoutMenuItem;
    private javax.swing.JButton produtoButton;
    private javax.swing.JMenuItem produtoMenuItem;
    private javax.swing.JMenuItem sairMenuItem;
    private javax.swing.JMenuItem sobreMenuItem;
    private javax.swing.JLabel usuarioLabel;
    private javax.swing.JMenuItem usuariosMenuItem;
    // End of variables declaration//GEN-END:variables
    private Action finallyApplicationAction;
    private Action clienteConsultaAction;
    private Action enderecoConsultaAction;
    private Action produtoConsultaAction;
    private Action categoriaConsultaAction;
    private Action usuarioConsultaAction;
    private Action contaUsuarioAction;
    private Action sobreAction;

    private void createActions() {
        finallyApplicationAction = new FinallyApplicationAction();
        clienteConsultaAction = new ClienteAction(true);
        enderecoConsultaAction = new EnderecoAction(true);
        produtoConsultaAction = new ProdutoAction(true);
        categoriaConsultaAction = new CategoriaAction(true);        
        usuarioConsultaAction = new UsuarioAction(true);
        contaUsuarioAction = new ContaUsuarioAction();
        sobreAction = new SobreAction();
    }

    class FinallyApplicationAction extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            closeApplication();
        }

        private void closeApplication() {
            if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, "Tem certeza de que deseja finalizar a aplicação?", "Confirmação", JOptionPane.YES_NO_OPTION)) {
                System.exit(0);
            }
        }
    }

    class ClienteAction extends AbstractAction {

        private boolean isConsult;

        public ClienteAction(boolean isConsult) {
            this.isConsult = isConsult;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (isConsult) {
                runConsulta();
            } else {
                runCadastro();
            }
        }

        private void runCadastro() {
            ClienteDialog dialog = new ClienteDialog(new javax.swing.JFrame(), true, StateEntityWindow.NEW, null);
            dialog.setVisible(true);
        }

        private void runConsulta() {
            ConsultaClienteDialog dialog = new ConsultaClienteDialog(new javax.swing.JFrame(), true, StateEntityWindow.SEARCH);
            dialog.setVisible(true);
        }
    }

    class EnderecoAction extends AbstractAction {

        private boolean isConsult;

        public EnderecoAction(boolean isConsult) {
            this.isConsult = isConsult;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (isConsult) {
                runConsulta();
            } else {
                runCadastro();
            }
        }

        private void runCadastro() {
            EnderecoDialog dialog = new EnderecoDialog(new javax.swing.JFrame(), true, StateEntityWindow.NEW, null);
            dialog.setVisible(true);
        }

        private void runConsulta() {
            ConsultaEnderecoDialog dialog = new ConsultaEnderecoDialog(new javax.swing.JFrame(), true, StateEntityWindow.SEARCH);
            dialog.setVisible(true);
        }
    }

    class ProdutoAction extends AbstractAction {

        private boolean isConsult;

        public ProdutoAction(boolean isConsult) {
            this.isConsult = isConsult;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (isConsult) {
                runConsulta();
            } else {
                runCadastro();
            }
        }

        private void runCadastro() {
            
        }

        private void runConsulta() {
            
        }
    }

    class CategoriaAction extends AbstractAction {

        private boolean isConsult;

        public CategoriaAction(boolean isConsult) {
            this.isConsult = isConsult;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (isConsult) {
                runConsulta();
            } else {
                runCadastro();
            }
        }

        private void runCadastro() {
            CategoriaDialog dialog = new CategoriaDialog(new javax.swing.JFrame(), true, StateEntityWindow.NEW, null);
            dialog.setVisible(true);
        }

        private void runConsulta() {
            ConsultaCategoriaDialog dialog = new ConsultaCategoriaDialog(new javax.swing.JFrame(), true, StateEntityWindow.SEARCH);
            dialog.setVisible(true);
        }
    }

    class UsuarioAction extends AbstractAction {

        private boolean isConsult;

        public UsuarioAction(boolean isConsult) {
            this.isConsult = isConsult;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (isConsult) {
                runConsulta();
            } else {
                runCadastro();
            }
        }

        private void runCadastro() {
            CreateUsuarioDialog dialog = new CreateUsuarioDialog(new javax.swing.JFrame(), true, StateEntityWindow.NEW);
            dialog.setVisible(true);
        }

        private void runConsulta() {
            ConsultaUsuarioDialog dialog = new ConsultaUsuarioDialog(new javax.swing.JFrame(), true, StateEntityWindow.SEARCH);
            dialog.setVisible(true);
        }
    }

    class ContaUsuarioAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent e) {
            ChangePasswordDialog dialog = new ChangePasswordDialog(new javax.swing.JFrame(), true, StateEntityWindow.CHANGEPASSWORD, getUser());
            dialog.setVisible(true);
        }
    }

    class SobreAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent e) {
            SobreDialog dialog = new SobreDialog(new javax.swing.JFrame(), true);
            dialog.setVisible(true);
        }
    }

    public Usuario getUser() {
        return user;
    }

    private void setUser(Usuario user) {
        this.user = user;
        if (user != null) {
            usuarioLabel.setText("Usuário logado no sistema: " + user.getLogin());
        }
    }

}
